<template>
	<view class="container">
		<view class="weixin-icon">
			<!-- 微信图标，可以使用图片或者字体图标 -->
			<text class="iconfont icon-weixin"></text>
		</view>
		<view class="auth-text">
			智问私有AI申请获取以下权限
		</view>
		<view class="auth-permission">
			获得你的公开信息(昵称，头像等)
		</view>
		<button class="auth-button" @click="authorizeLogin">点击授权</button>
	</view>
</template>

<script>
	import request from "../../utils/request.js"
	export default {
		methods: {
			async authorizeLogin() {
				// 调用微信登录
				await uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						if (loginRes.code) {
							console.log(loginRes)
							// 调用获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: (infoRes) => {
									// 处理获取到的用户信息
									console.log(infoRes.userInfo);
									// 可以在这里进行页面跳转或者发送数据到服务器
								},
								fail: (err) => {
									console.error('获取用户信息失败', err);
								}
							});
							request.post('/Login1',{
								code: loginRes.code
							},{
								// headers: { 'content-type': 'application/x-www-form-urlencoded' },
								headers: {
									'content-type': 'application/json'
								},
							}).then(res => {
								console.log(res.data.msg)
								uni.setStorageSync("token", res.data.msg)
								//localStorage.setItem("token" , res.data);
								uni.reLaunch({
									url: '/pages/index/index'
								}).catch(error => {
									console.log(error)
								})
							})
						} else {
							console.error('获取登录凭证失败', loginRes);
						}
					},
					fail: (err) => {
						console.error('登录失败', err);
					}
				});
				// uni.reLaunch({
				// 	url: '/pages/index/index'
				// })
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
	}

	.weixin-icon {
		font-size: 100px;
		margin-bottom: 20px;
	}

	.auth-text,
	.auth-permission {
		text-align: center;
		margin-bottom: 20px;
	}

	.auth-button {
		background-color: #1AAD19;
		color: white;
		padding: 10px 20px;
		border-radius: 5px;
	}
</style>